﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SFX
{
    public class Player
    {
        public int Health = 100;
        public int Kills = 0;
        public int Deaths = 0;
        public int Hits = 0;
        public int Shots = 0;
        public float Accuracy = 0;
        public Objects.Tank PlayerTank;
        public bool PlayerActive = false;
        public int PlayerNumber;
        public Player(int playerNumber)
        {
            PlayerNumber =playerNumber;
            PlayerTank = new Objects.Tank(playerNumber);
            PlayerActive = false;
        }
    }
    public class SessionManager
    {
        public Player Player1, Player2, Player3, Player4;
        public int DeathMatchMaxKills = 10;
        public bool GameOver = false;
        int MatchWinner = 0;
        public SessionManager()
        {
            Player1 = new Player(1);

            Player2 = new Player(2);

            Player3 = new Player(3);

            Player4 = new Player(4);
        }

        public void InitializeTanks()
        {
            if (Player1.PlayerActive)
            {
                Player1.PlayerTank.Image = States.GameplayState.theTextureManager.GetTexture("TANK1");
                Player1.PlayerTank.TurretImage = States.GameplayState.theTextureManager.GetTexture("TANK1TURRET");
                Player1.PlayerTank.CollisionRect = new Rectangle((int)Player1.PlayerTank.Position.X, (int)Player1.PlayerTank.Position.Y, Player1.PlayerTank.Image.Width, Player1.PlayerTank.Image.Height);
            }
            if (Player2.PlayerActive)
            {
                Player2.PlayerTank.Image = States.GameplayState.theTextureManager.GetTexture("TANK2");
                Player2.PlayerTank.TurretImage = States.GameplayState.theTextureManager.GetTexture("TANK2TURRET");
                Rectangle collision = new Rectangle((int)Player2.PlayerTank.Position.X, (int)Player2.PlayerTank.Position.Y, Player2.PlayerTank.Image.Width, Player2.PlayerTank.Image.Height);
                Player2.PlayerTank.CollisionRect = collision;

            }
            if (Player3.PlayerActive)
            {
                Player3.PlayerTank.Image = States.GameplayState.theTextureManager.GetTexture("TANK3");
                Player3.PlayerTank.TurretImage = States.GameplayState.theTextureManager.GetTexture("TANK3TURRET");
                Rectangle collision = new Rectangle((int)Player3.PlayerTank.Position.X, (int)Player3.PlayerTank.Position.Y, Player3.PlayerTank.Image.Width, Player3.PlayerTank.Image.Height);
                Player3.PlayerTank.CollisionRect = collision;

            }
            if (Player4.PlayerActive)
            {
                Player4.PlayerTank.Image = States.GameplayState.theTextureManager.GetTexture("TANK4");
                Player4.PlayerTank.TurretImage = States.GameplayState.theTextureManager.GetTexture("TANK4TURRET");

                Rectangle collision = new Rectangle((int)Player4.PlayerTank.Position.X, (int)Player4.PlayerTank.Position.Y, Player4.PlayerTank.Image.Width, Player4.PlayerTank.Image.Height);
                Player4.PlayerTank.CollisionRect = collision;
            }

        }
        public void AddKills(int Player, int Value)
        {
            if (Player == 1)
                Player1.Kills += Value;
            if (Player == 2)
                Player2.Kills += Value;
            if (Player == 3)
                Player3.Kills += Value;
            if (Player == 4)
                Player4.Kills += Value;
        }
        public void AddDeaths(int Player)
        {
            if (Player == 1)
                Player1.Deaths += 1;
            if (Player == 2)
                Player2.Deaths += 1;
            if (Player == 3)
                Player3.Deaths += 1;
            if (Player == 4)
                Player4.Deaths += 1;
        }
        public void RespawnTank(int Player)
        {
            if (Player == 1)
            {
                Player1.Health = 100;
                Player1.PlayerTank.Position = new Vector2(100, 150);
            }
            if (Player == 2)
            {
                Player2.Health = 100;
                Player2.PlayerTank.Position = new Vector2(1720, 150);
            }
            if (Player == 3)
            {
                Player3.Health = 100;
                Player3.PlayerTank.Position = new Vector2(100, 860);
            }
            if (Player == 4)
            {
                Player4.Health = 100;
                Player4.PlayerTank.Position = new Vector2(1720, 860);
            }
        }
        public int GetTankHealth(int Player)
        {
            if (Player == 1)
                return Player1.Health;
            if (Player == 2)
                return Player2.Health;
            if (Player == 3)
                return Player3.Health;
            if (Player == 4)
                return Player4.Health;

            return 0;
        }
        public void SetHealth(int Player, int Value)
        {
            if (Player == 1)
                Player1.Health = Value;
            if (Player == 2)
                Player2.Health = Value;
            if (Player == 3)
                Player3.Health = Value;
            if (Player == 4)
                Player4.Health = Value;
        }
        public void DecreaseHealth(int Player, int Value)
        {
            if (Player == 1)
                Player1.Health -= Value;
            if (Player == 2)
                Player2.Health -= Value;
            if (Player == 3)
                Player3.Health -= Value;
            if (Player == 4)
                Player4.Health -= Value;
            
        }
        public void AddShots(int playerNumber)
        {
            if (playerNumber == 1)
            {
                Player1.Shots += 1;
            }
            if (playerNumber == 2)
            {
                Player2.Shots += 1;
            }
            if (playerNumber == 3)
            {
                Player3.Shots += 1;
            }
            if (playerNumber == 4)
            {
                Player4.Shots += 1;
            }
        }
        public void AddHits(int playerNumber)
        {
            if (playerNumber == 1)
            {
                Player1.Hits += 1;
            }
            if (playerNumber == 2)
            {
                Player2.Hits += 1;
            }
            if (playerNumber == 3)
            {
                Player3.Hits += 1;
            }
            if (playerNumber == 4)
            {
                Player4.Hits += 1;
            }
        }
        public void CheckScores()
        {
            if (Player1.Kills >= DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 1;
            }
            else if (Player2.Kills >= DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 2;
            }
            else if (Player3.Kills >= DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 3;
            }
            else if (Player4.Kills >= DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 4;
            }
        }

        public void UpdatePlayers(float ElapsedTime)
        {
            CheckScores();
            if (Player1.PlayerActive)
            {
                if (Player1.Shots > 0)
                    Player1.Accuracy = ((float)Player1.Hits / (float)Player1.Shots) * 100;
            }
            if (Player2.PlayerActive)
            {
                if (Player2.Shots > 0)
                    Player2.Accuracy = ((float)Player2.Hits / (float)Player2.Shots) * 100;
            }
            if (Player3.PlayerActive)
            {
                if (Player3.Shots > 0)
                    Player3.Accuracy = ((float)Player3.Hits / (float)Player3.Shots) * 100;
            }
            if (Player4.PlayerActive)
            {
                if (Player4.Shots > 0)
                    Player4.Accuracy = ((float)Player4.Hits / (float)Player4.Shots) * 100;
            }
            if (!GameOver)
            {
                if (Player1 != null && Player1.PlayerActive)
                {
                    Player1.PlayerTank.Update(ElapsedTime);
                   
                }

                if (Player2 != null && Player2.PlayerActive)
                {
                    Player2.PlayerTank.Update(ElapsedTime);
                    
                }

                if (Player3 != null && Player3.PlayerActive)
                {
                    Player3.PlayerTank.Update(ElapsedTime);
                    
                }

                if (Player4 != null && Player4.PlayerActive)
                {
                    Player4.PlayerTank.Update(ElapsedTime);
                    
                }
            }
        }


        public void CheckScores(float ElapsedTime)
        {
            if (Player1.Kills == DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 1;
            }
            else if (Player2.Kills == DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 2;
            }
            else if (Player3.Kills == DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 3;
            }
            else if (Player4.Kills == DeathMatchMaxKills)
            {
                GameOver = true;
                MatchWinner = 4;
            }
        }
        public void RenderPlayers(float ElapsedTime)
        {
            if (Player1 != null && Player1.PlayerActive)
                Player1.PlayerTank.Render(ElapsedTime, RoyalRumble.MainGame.spriteBatch);

            if (Player2 != null && Player2.PlayerActive)
                Player2.PlayerTank.Render(ElapsedTime, RoyalRumble.MainGame.spriteBatch);

            if (Player3 != null && Player3.PlayerActive)
                Player3.PlayerTank.Render(ElapsedTime, RoyalRumble.MainGame.spriteBatch);

            if (Player4 != null && Player4.PlayerActive)
                Player4.PlayerTank.Render(ElapsedTime, RoyalRumble.MainGame.spriteBatch);

        }
    }
}